# Contributor: Michael Pirogov <vbnet.ru@gmail.com>
# Maintainer: Michael Pirogov <vbnet.ru@gmail.com>
pkgname=lizardfs
pkgver=3.13.0
pkgrel=13
pkgdesc="Highly reliable, scalable and efficient distributed file system"
url="https://github.com/lizardfs/lizardfs/"
arch="all"
license="GPL-3.0-only"
makedepends="
	asciidoc
	boost-dev
	cmake
	db-dev
	fuse-dev
	fuse3-dev
	isa-l-dev
	judy-dev
	libpcap-dev
	libtool
	linux-pam-dev
	python3-dev
	samurai
	spdlog-dev
	thrift-dev
	zlib-dev
	"
checkdepends="gtest-dev"
pkgusers="mfs"
pkggroups="mfs"
options="!check" # need to setup a virtual machine
install="$pkgname.pre-install"
subpackages="
	$pkgname-doc
	$pkgname-client
	$pkgname-master
	$pkgname-chunkserver
	$pkgname-metalogger
	$pkgname-cgi::noarch
	$pkgname-cgiserv::noarch
	$pkgname-metalogger-openrc:metalogger_openrc
	$pkgname-master-openrc:master_openrc
	$pkgname-chunkserver-openrc:chunkserver_openrc
	$pkgname-cgiserv-openrc:cgiserv_openrc
	$pkgname-bash-completion
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/lizardfs/lizardfs/archive/refs/tags/$pkgver-rc3.tar.gz
	gcc13.patch
	$pkgname-uraft.patch
	$pkgname-cgiserv.initd
	$pkgname-chunkserver.initd
	$pkgname-cgiserv.confd
	$pkgname-master.initd
	$pkgname-metalogger.initd
	$pkgname-iostat-header.patch
	$pkgname-cstdio-header.patch
	"
builddir="$srcdir/$pkgname-$pkgver-rc3"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DENABLE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DENABLE_DOCS=NO \
		-DENABLE_JEMALLOC=YES \
		-DCMAKE_INSTALL_PREFIX=/ \
		-DENABLE_POLONAISE=OFF \
		-DTHROW_INSTEAD_OF_ABORT=YES \
		-Wno-dev \
		$CMAKE_CROSSOPTS
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	chown -R mfs:mfs "$pkgdir"/var/lib/lizardfs

	mkdir -p "$pkgdir"/usr/share/bash-completion
	mv "$pkgdir"/etc/bash_completion.d "$pkgdir"/usr/share/bash-completion/completions
}

master() {
	pkgdesc="LizardFS master server"
	depends="lizardfs=$pkgver-r$pkgrel"
	amove \
		usr/sbin/mfsmaster \
		usr/sbin/mfsrestoremaster \
		usr/sbin/mfsmetadump \
		usr/sbin/mfsmetarestore \
		var/lib/lizardfs/metadata.mfs.empty
	chown -R $pkgusers:$pkggroups "$subpkgdir"/var/lib/lizardfs
}

master_openrc() {
	default_openrc
	install -Dm755 "$srcdir"/$pkgname-master.initd "$subpkgdir"/etc/init.d/$pkgname-master
}

metalogger() {
	pkgdesc="LizardFS metadata replication server"
	depends="lizardfs=$pkgver-r$pkgrel"
	amove usr/sbin/mfsmetalogger
}

metalogger_openrc() {
	default_openrc
	install -Dm755 "$srcdir"/$pkgname-metalogger.initd "$subpkgdir"/etc/init.d/$pkgname-metalogger
}

chunkserver() {
	pkgdesc="LizardFS data server"
	depends="lizardfs=$pkgver-r$pkgrel"
	amove usr/sbin/mfschunkserver
}

chunkserver_openrc() {
	default_openrc
	install -Dm755 "$srcdir"/$pkgname-chunkserver.initd "$subpkgdir"/etc/init.d/$pkgname-chunkserver
}

client() {
	pkgdesc="LizardFS client"
	depends="fuse fuse3"
	amove usr/bin/*

}

cgi() {
	pkgdesc="LizardFS CGI Monitor"
	depends="python3"
	amove usr/share/mfscgi
}

cgiserv() {
	pkgdesc="Simple CGI-capable HTTP server to run LizardFS CGI Monitor"
	depends="$pkgname-cgi=$pkgver-r$pkgrel"
	amove usr/sbin/mfscgiserv
}

cgiserv_openrc() {
	default_openrc
	install -Dm755 "$srcdir"/$pkgname-cgiserv.initd "$subpkgdir"/etc/init.d/$pkgname-cgiserv
	install -Dm755 "$srcdir"/$pkgname-cgiserv.confd "$subpkgdir"/etc/conf.d/$pkgname-cgiserv
}

sha512sums="
5e57c0704d30d1e4f7fd7c2ac6ed889d0075d86ad20536fc927f4fe0d228b907c4ac2697d3c17d0313853f27506fdf6441bf9559d3ed79ae95f51595ec3363d9  lizardfs-3.13.0.tar.gz
b0c13076aeef2489897ab73138101fbbd239ae4c7375bb45dc280b42dd5d7a761a46a97b64f3b5274c99a4318758024db593cb91a0d9a82e58b71ef45b20bf4c  gcc13.patch
ec38f06048c2719bba1a8e751411468270e9bc82c955c897dde21cd1e428be686e2b3ae680a57813370ec89e264f66132acd27f84f2ade6f5a63c0b8e2ece256  lizardfs-uraft.patch
c698aff4de9aeb76202a809e44ac8d0ec9c6348a806b1c813c2a40858339b1c139a480a9c5aff40bf2c9821883c6c0dfeabb010f3faa5746673235f0fb3c5a76  lizardfs-cgiserv.initd
56a714f3d29030ca12dc1859a1121b2a89606a2ccccec65481e2eb0b846757dea71dfe5f5a6d648b32e731d14b7d7725f79703851365b0862d7eb415a5f49dfb  lizardfs-chunkserver.initd
b5c625f0004df33889de60ddad37e41f3acf081b37247606a1544e5f63354e121fe4cce511a6e60f4f2c0305155faf0614b8a4bce7267929fe68a1a4b546b582  lizardfs-cgiserv.confd
a041fa324d37bda098ad65e9d6507f281ed388471956ca79aa33b8b0d1c4a9d528662a2410c47f3856183d6378ac7fb417c3d7ec314f624e7e5dac7c5e4247f0  lizardfs-master.initd
8e73a7715e7a010d435efb9058ed67aad1caa95c840d3f1fae6bafd2a700cedbb0244d29363929d8e8d9a2036a2e0b173c0c2f7b52529ba4088e1296479fc0f5  lizardfs-metalogger.initd
fed14323975cd2d916bcc78d15ec105176106024faa6587a5b6dc3f2c7c5cf3632491babf2865459ac614ba885ee79e301aac4f19c27c4f04780a4a9d94e9312  lizardfs-iostat-header.patch
b91f07af8a1d82ac9530e0ddc5984ee1f3cba9af7d6e9bf2093d04fbd0862d98860a2db2bd13fe6e0eea0cd7bfd2254a8973a5a398402c1682c51b54267b82cf  lizardfs-cstdio-header.patch
"
